<?php

/*
 * 手机号和微信号绑定
 */
namespace Mobile\Controller;
use Mobile\Model\smscodeModel;

class CheckController extends BaseController {
    
    public $smscodeModel;
    
    public function __construct() {
        parent::__construct();
        layout("Mui/layout");
        $this->smscodeModel = new smscodeModel();
        
        if(!$this->openid || empty($this->wxuserinfo)){
            if(IS_AJAX){
                return_json(1,"操作过时,请在微信客户端刷新重试");
            }
            $this->error2("请在微信客户端中打开~");
        }
        
        //判断当前手机号是否已经绑定手机账号
        $where = array(
            "openid" => $this->openid,
        );
        
        $res = $this->memberModel->getMember($where);
        if($res['mobile']){
            if(!IS_AJAX){
                $this->error2("当前微信号已验证手机号:".$res['mobile'],"/mobile");
            }else{
                return_json(1,"当前微信号已验证手机号:".$res['mobile'],"","/mobile");
            }
        }
       
        
    }
    

    /*
     * 首页
     */
    public function mobile(){

        $seo_data = array(
            "title" => "手机验证-".C('SITE_NAME'),
            "description" => "",
            "keywords" => "",
        );
        $this->setSeo($seo_data);
        
        $share_data = array(
            "title" => "手机验证-".C('SITE_NAME'),
        );
        $this->setShare($share_data);
        
        $this->assign('no_nav',true);//不显示底部菜单
        $this->assign('muiactive',1);
        
        $this->display();
    }
    
    
    /*
     * 发送手机验证码
     */
    public function sendsms(){
       
        $mobile = get('mobile');
        if(!check_mobile($mobile)){
            return_json(1,"请检查手机号是否正确");
        }
        
        if(!IS_AJAX){
            return_json(1,"提交方式有误");
        }
        
        //判断当前手机号是否已经被别人使用
        $where = array(
            "mobile" => $mobile,
        );
        
        $res = $this->memberModel->getMember($where);
        if($res){
            if($res['openid'] && ($this->openid == $res['openid'])){
                return_json(1,"你已经验证通过,无需再次验证","","/mobile/");
            }else if($res['openid'] && ($this->openid != $res['openid'])){
                return_json(1,"当前号码已被其他微信绑定,请更换手机号或解除绑定".$this->openid);
            }
        }else{
            return_json(1,"抱歉,您的信息暂未录入系统,请联系客服为您录入");
        }
        
        //只允许CEO级别以上的人录入系统
        if($res['grade_id']<2){
           // return_json(1,"抱歉,暂时只允许CEO、皇冠、大皇冠使用手机商城哦~。如您已达条件,请联系客服为您处理");
        }
        
        
        //判断是否超出频率限制
        //短信验证码，使用同一个签名，对同一个手机号码发送短信验证码，允许每分钟1条，累计每小时7条。
        $time1 = date("Y-m-d H:i:s");//当前时间
        $time2 = date("Y-m-d H:i:s",time()-60);//60秒前
        $time3 = date("Y-m-d H:i:s",time()-60*60);//1小时前
        $today1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d")));//今天的时间
        

        $where_min = array(
            "mobile" => $mobile,
            "send_status" => 1,
            "create_time" => array(
                "between" => array(
                    $time1,$time2
                )
            )
        );
        $count_min = $this->smscodeModel->getSmscodeCount($where_min);
        if($count_min>=1){
            return_json(1,"请稍等60秒后重新获取");
        }
        
        $where_hour = array(
            "mobile" => $mobile,
            "send_status" => 1,
            "create_time" => array(
                "between" => array(
                    $time1,$time3
                )
            )
        );
        $count_hour = $this->smscodeModel->getSmscodeCount($where_hour);
        if($count_hour>=7){
            return_json(1,"请稍等1个小时后重新获取");
        }
       
        
        //发送短信
        $code = rand(1000,9999);
        $data = array(
            "code" => "$code",
            "product" => "爱渡活力餐"
        );
        
        //将信息存储到数据库
        $save_data = array(
            "uid" => $this->uid,
            "smssign" => C('ALIDAYU_SIGN'),
            "mobile" => $mobile,
            "tmpid" => C('ALIDAYU_SHENFEN_TMP'),
            "content" => json_encode($data),
            "code" => $code,
            "create_time" => date("Y-m-d H:i:s"),
            "expired_time" => date("Y-m-d H:i:s",time()+5*60),
            "ip" => get_client_ip(),
            "try_time" => 0,
            "status" => 1,
        );
        
        $res = alisms(C('ALIDAYU_SIGN'),$mobile,C('ALIDAYU_SHENFEN_TMP'),$data);
       
        if($res['result']['success']){
            //发送成功
            //存储数据
            $save_data['send_status'] = 1;
            $code_id = $this->smscodeModel->addSmscode($save_data);
            return_json(0,"",$code_id);
        }else{
            //发送失败
            //echo $resp['result']['err_code'];
            $msg = "发送失败,请稍后重试。错误原因:".$res['sub_msg'];
            $save_data['send_status'] = 0;
            $this->smscodeModel->addSmscode($save_data);
            return_json(1,$msg);
            //短信验证码，使用同一个签名，对同一个手机号码发送短信验证码，允许每分钟1条，累计每小时7条。
            //短信通知，使用同一签名、同一模板，对同一手机号发送短信通知，允许每天50条（自然日）。
        }
        
    }
    
    
    /*
     * 验证短信是否正确
     */
    public function checkcode(){
        
        if(!IS_AJAX){
            return_json(1,"提交方式有误");
        }
        
        $code_id = post('code_id');
        $code = post("code");
        $mobile = post("mobile");
        $ip = get_client_ip();
        
        if($code == ''){
            return_json(1,"验证码填写错误");
        }
        
        $where = array(
            "code_id" => $code_id,
            "code" => $code,
            "mobile" => $mobile,
        );
        
        $res = $this->smscodeModel->getSmscode($where);
        if(!$res){
            //将尝试次数加1
            $this->smscodeModel->where(array("code_id"=>$code_id))->setInc('try_time');//将尝试次数加1
            return_json(1,"验证码错误");
        }
        
        if($res['ip'] != $ip){
          //  $this->smscodeModel->where(array("code_id"=>$code_id))->setInc('try_time');//将尝试次数加1
          //  return_json(1,"你的网络有风险,已阻止此次验证!");
        }
        
        if(strtotime($res['expired_time']) < time()){
            return_json(1,"验证码已过期,请重新获取!");
        }
        
        if($res['try_time'] > 2){
            return_json(1,"验证码已失效,请重新获取!");
        }
        
        if($res['status'] != 1){
            return_json(1,"验证码已失效,请重新获取!");
        }
        
        //验证通过
        //将验证码作废
        $this->smscodeModel->where(array("code_id"=>$code_id))->setInc('try_time');//将尝试次数加1
        $this->smscodeModel->where(array("code_id"=>$code_id))->setField('status',0);//将尝试次数加1
        
        //将电话号码和微信号绑定
        
        //判断当前微信是否已经存在系统中
        $where_openid = array(
            "openid" => $this->openid,
        );
        $res_openid = $this->memberModel->getMember($where_openid);
        
        //判断当前手机号是否存在系统中
        $where_mobile = array(
            "mobile" => $mobile,
        );
        $res_mobile = $this->memberModel->getMember($where_mobile);
        if($res_mobile){
            //电话号码已存在系统中
            
            $new_data = $this->wxuserinfo;
            $new_data['mobile_check'] = 1;
            
            if($res_openid){
                
                //微信资料也存在系统中,
                if(!$res_mobile['openid']){
                    //如果该手机号尚未绑定微信,删除微信账号,并把微信资料写入该手机账户
                    //判断该微信号是否有余额,如果有余额则不允许删除
                    if($res_openid['money']>0 || $res_openid['score']>0 || $res_openid['cash_money']>0 || $res_openid['tihuo_money']>0){
                        return_json(1,"你当前的微信账号中尚有余额资产,为了你的资金安全,不允许和已有的手机账号合并","","/mobile/");
                    }
                    
                    //判断两次推荐人是否一致
                    if($res_mobile['refer_uid']>0 && ($res_openid['refer_uid'] != $res_mobile['refer_uid'])){
                        if($res_openid['refer_uid'] != C('DEFAULT_SUID')){
                            //不是默认推荐人
                            return_json(1,"你当前的微信账号和手机账号的推荐人不一致,不允许合并","","/mobile/");
                        }
                    }
                    //删除微信账号
                    $where_del = array(
                        "openid" => $this->openid
                    );
                    $res = $this->memberModel->where($where_del)->delete();
                    if($res){
                        //删除成功
                        //将微信资料写入该手机号用户中
                        $this->uid = $res_mobile['uid'];
                        $this->updateMemberInfoByUid($res_mobile['uid'], $new_data);
                    }
                    
                    
                }else if($res_mobile['openid'] == $res_openid['openid']){
                    //该手机号已经绑定该微信
                    return_json(1,"你已经验证通过,无需再次验证","","/mobile/");
                }else{
                    return_json(1,"当前微信和手机均已注册账号,无法绑定","","/mobile/");
                }
                
            }else{
                //微信资料尚未入库,将微信资料写入该手机号用户中
                $this->uid = $res_mobile['uid'];
                $new_data['create_time'] = date("Y-m-d H:i:s"); 
                $this->updateMemberInfoByUid($res_mobile['uid'], $new_data);
            }
        }else{
            
            return_json(1,"抱歉,您的信息暂未录入系统,请联系客服为您录入");
            
            //电话号码不在系统中
            /*
            if($res_openid){
                //微信资料存在系统中,将此微信的手机号码更改为新号码
                $this->uid = $res_openid['uid'];
                $mobile_data = array(
                    "mobile" => $mobile,
                    "mobile_check" => 1
                );
                $this->updateMemberInfoByUid($res_openid['uid'], $mobile_data);
            }else{
                //微信资料尚未入库,将微信资料 手机号 写入一个新账号中
                $new_data = $this->wxuserinfo;
                $new_data['mobile'] =  $mobile;
                $new_data['mobile_check'] =  1;
                $new_data['status'] = 1;
                $new_data['last_login_ip'] = get_client_ip();
                $new_data['last_login_time'] = date("Y-m-d H:i:s");
                $new_data['login_times'] = 1;
                $new_data['create_time'] = date("Y-m-d H:i:s");
                $new_data['role_id'] = 2;
                $new_data['is_change_username'] = 0;
                
                $this->uid = $this->memberModel->addMember($new_data);
            }
            */
            
            
            
        }
        
        //刷新session
        $this->updateMemberSession($this->uid);
        return_json(0,"已登录,请稍后...","","/mobile/");
    }
    

    
    
}